//Given an m x n binary matrix filled with 0's and 1's, find the largest square 
//containing only 1's and return its area. 
//
// 
// Example 1: 
//
// 
//Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1",
//"1"],["1","0","0","1","0"]]
//Output: 4
// 
//
// Example 2: 
//
// 
//Input: matrix = [["0","1"],["1","0"]]
//Output: 1
// 
//
// Example 3: 
//
// 
//Input: matrix = [["0"]]
//Output: 0
// 
//
// 
// Constraints: 
//
// 
// m == matrix.length 
// n == matrix[i].length 
// 1 <= m, n <= 300 
// matrix[i][j] is '0' or '1'. 
// 
// Related Topics Array Dynamic Programming Matrix 👍 5566 👎 123


package leetcode.editor.en;

public class _221_MaximalSquare {
    public static void main(String[] args) {
        Solution solution = new _221_MaximalSquare().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maximalSquare(char[][] matrix) {
            if (matrix ==null) {
                return 0;
            }
            int height = matrix.length;
            int width = matrix[0].length;
            int max = 0;
            int[][] dp = new int[height+1][width+1];
            for (int row = 0; row < height; row++) {
                for (int col = 0; col < width; col++) {
                    if (matrix[row][col]=='1') {
                        dp[row+1][col+1] = Math.min(Math.min(dp[row][col], dp[row][col+1]), dp[row+1][col]) +1;
                        max = Math.max(max, dp[row+1][col+1]);
                    }
                }
            }
            return max*max;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}